let baseUrl = 'https://houtai.boliankeji.xyz/'  //请求接口地址
const httpRequest = ({url, method, data, hideLoading}) => {
    let meth = method.toUpperCase();//小写改为大写
    if (!data) {
        data = {}
    }
    if (getApp().globalData.token) {
        data['token'] = getApp().globalData.token; //token
    }
    // if (wx.currentDisability) {
    //     baseUrl = wx.currentDisability.url.lastIndexOf('/').length === wx.currentDisability.url.length ? wx.currentDisability.url : wx.currentDisability.url + '/'
    // } else if (!customBaseUrl) {
    //     return;
    // }
    if (hideLoading) {
        uni.showLoading({title: '加载中...', mask: true});
    }
    // meth == 'GET' ? 'application/x-www-form-urlencoded' :
    let httpDefaultOpts = {
        url: baseUrl + url,
        data: data,
        method: meth,
        header: {
            'Content-Type': 'application/json',
        },
        dataType: 'json',
    }
    return new Promise(function (resolve, reject) {
        uni.request(httpDefaultOpts).then(
            (res) => {
                if (hideLoading) {
                    uni.hideLoading();
                }
                let resultData = res[1]['data']
                if (resultData.code == 0 || resultData.code == 1 || resultData.code == "-2") {//成功返回
                    resolve(resultData)
                } else {//错误信息
                    uni.showToast({
                        title: resultData.msg,
                        icon: 'none'
                    })
                    reject(resultData)
                }
            }
        ).catch(
            (response) => {
                if (!hideLoading) {
                    uni.hideLoading();
                }
                reject(response)
            }
        )
    })
};
// post 请求
const post = (url, data, hideLoading = true) => {

    return httpRequest({url, method: 'POST', data, hideLoading})
}
// get 请求
const get = (url, data, hideLoading = true) => {
    return httpRequest({url, method: 'GET', data, hideLoading})
}


export default {
    baseUrl,
    httpRequest,
    post,
    get,
}
